<template>
  <div>
    <div style="display: flex">
      <el-input
          size="small"
          placeholder="添加职称等级.."
          prefix-icon="el-icon-plus"
          style="width: 300px"
          v-model="jl.name">
      </el-input>
      <el-select v-model="jl.titleLevel" placeholder="请选择等级" style="margin: 0 8px"
                 size="small">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" @click="addTitleLevel">添加</el-button>
    </div>
    <div>
      <el-table
          border
          stripe
          :data="jobLevels"
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 80%; margin: 8px 0">
        <el-table-column
            type="selection"
            size="small"
        >
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="45">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称等级"
            width="150">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建日期"
            width="150">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="90">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled == true">已启用</el-tag>
            <el-tag type="danger" v-if="scope.row.enabled == false">未启用</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showUpdateEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px ; display: flex">
        <el-button @click="deleteMore" small type="danger" :disabled="this.multipleSelection.length == 0">批量删除
        </el-button>
        <el-button @click="noDeleteMore">取消选择</el-button>
      </div>
    </div>
    <el-dialog
        title="编辑职称"
        :visible.sync="dialogVisible"
        width="25%"
    >
      <span class="dialogSpan" style="display: flex">
        <table>
          <tr>
            <td>
              <el-tag>职称</el-tag>
            </td>
            <td>
              <el-input size="small" v-model="updateJl.name" style="margin: 0 8px;width: 160px"></el-input>
            </td>
          </tr>
          <tr style="margin: 8px 0">
            <td>
              <el-tag>等级</el-tag>
            </td>
            <td>
              <el-select v-model="updateJl.titleLevel" placeholder="请选择等级" style="margin: 0 8px;width: 160px"
                         size="small">
                <el-option
                    v-for="item in titleLevels"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>是否启用</el-tag>
            </td>
            <td>
              <el-switch
                  v-model="updateJl.enabled"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="已启用"
                  inactive-text="未启用">
              </el-switch>
            </td>
          </tr>
        </table>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noUpdate" size="small">取 消</el-button>
        <el-button type="primary" @click="doUpdate" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data() {
    return {
      multipleSelection: [],
      dialogVisible: false,
      jl: {
        name: '',
        titleLevel: ''
      },
      updateJl: {
        name: '',
        titleLevel: '',
        enabled:false
      },
      titleLevels: [
        "正高级",
        "副高级",
        "中级",
        "初级",
        "员级"
      ],
      jobLevels: []
    }
  },
  mounted() {
    this.initJobLevel();
  },
  methods: {
    noDeleteMore() {
      this.multipleSelection = [];
      this.initJobLevel();
    },
    deleteMore() {
      this.$confirm('此操作将删除[ ' + this.multipleSelection.length + ' ]条职位数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          ids = ids + 'ids=' + item.id + '&'
        })
        this.deleteRequest('/api/system/basic/joblevel/' + ids).then(resp => {
          if (resp) {
            this.initJobLevel();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete(index, date) {
      this.$confirm('此操作将删除 [ ' + date.name + ' ] 职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/api/system/basic/joblevel/' + date.id).then(resp => {
          if (resp) {
            this.initJobLevel();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    doUpdate() {
      this.putRequest('/api/system/basic/joblevel/', this.updateJl).then(resp => {
        if (resp) {
          this.initJobLevel();
          this.dialogVisible = false;
        }
      })
    },
    noUpdate() {
      this.dialogVisible = false;
      this.initJobLevel();
    },
    showUpdateEdit(index, date) {
      Object.assign(this.updateJl,date);
      this.dialogVisible = true;
    },
    addTitleLevel() {
      if (this.jl.name && this.jl.titleLevel) {
        this.postRequest('/api/system/basic/joblevel/', this.jl).then(resp => {
          if (resp) {
            this.initJobLevel();
            this.jl.name = '';
            this.jl.titleLevel = '';
          }
        })
      } else {
        this.$message.error('职称或等级不能为空！');
      }
    },

    initJobLevel() {
      this.getRequest('/api/system/basic/joblevel/').then(resp => {
        if (resp) {
          this.jobLevels = resp;
        }
      })
    }
  }

}
</script>

<style scoped>

</style>